import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:kq_flutter_core_widget/utils/toast_util.dart';

enum FileType {
  ///text文件格式
  txt,
  html
}

/// 文件编码处理工具，目前针对txt文件的编码
class FileUnicodeNativeUtil {
  static const _methodChannelName = 'flutter.keqiang.com/fileUnicode';

  static const _platformMethod = MethodChannel(_methodChannelName);

  /// 文件编码，默认text文件
  static Future<String?> fileUnicode(String fileCachePath,
      {FileType? type}) async {
    try {
      var result = await _platformMethod.invokeMethod('fileUnicode', {
        'path': fileCachePath,
        'fileType': type != null && type == FileType.html ? 'html' : 'txt'
      });
      if (result['code'] == 1) {
        String e = result['data'];
        return e;
      } else if (result['code'] == 0) {
        var msg = result['msg'];
        KqToast.showNormal(msg);
      }
      return null;
    } on PlatformException catch (e) {
      debugPrint(e.message);
      return null;
    }
  }
}
